package org.example.practice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.*;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.dto.PracticeDto;
import org.example.commonapi.dto.PracticeInfoDto;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.oss.OssService;
import org.example.practice.service.PracticeHistoryService;
import org.example.practice.service.PracticeImageService;
import org.example.practice.service.PracticeService;
import org.example.practice.service.SubjectService;
import org.example.practice.until.PracticeUntil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author XieNoDesire
 * todo  差一个ai
 */
@RestController
@RequestMapping("/practice")
@Slf4j
public class PracticeController {

    @Resource
    PracticeService practiceService;

    @Resource
    JwtUtil jwtUtil;

    @Resource
    PracticeImageService practiceImageService;

    @Resource
    OssService ossService;

    @Resource
    PracticeHistoryService practiceHistoryService;

    @Resource
    SubjectService subjectService;

    /**
     * 公共模块查询试题 (type为0的时候搜的出来type=1的数据)-->已解决
     * @param difficulty
     * @param type
     * @param name
     * @param subject
     * @return success
     */
    @GetMapping("/publicList")
    public R publicList(Integer difficulty, Integer type, String name, String subject){
        log.info("difficulty:{},type:{},name:{},subject:{}",difficulty,type,name,subject);
        List<PracticeInfoDto> list= practiceService.list(difficulty,type,name,subject);
        log.info("公共试题:{}",list);
        return R.success(list);
    }


    /**
     * 我创建的试题
     * @param difficulty
     * @param type
     * @param name
     * @param subject
     * @param request
     * @return success
     */
    @GetMapping("/privateList")
    public R privateList(Integer difficulty, Integer type, String name, String subject, HttpServletRequest request){
        log.info("difficulty:{},type:{},name:{},subject:{}",difficulty,type,name,subject);
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        List<PracticeInfoDto> list= practiceService.list(difficulty,type,name,subject,loginInfo.getId());
        log.info("我创建的试题:{}",list);
        return R.success(list);
    }

    /**
     * 查看单个题目内容 success
     * @param id 题目id
     * @return
     */
    @GetMapping("/{id}")
    public R getOne(@PathVariable("id") Long id){
        PracticeDto practice=new PracticeDto(practiceService.getOne(id)) ;
        List<String> images=practiceImageService.imageList(id);
        practice.setImages(images);
        log.info("单个题目内容:{}",practice);
        return R.success(practice);
    }

    /**
     * 添加题目 success
     * @param practice
     * @param request
     * @return
     */
    @PostMapping("/insert")
    public R insert(@RequestBody Practice practice, HttpServletRequest request){
//        查看题目是否合规
        R errorResult= PracticeUntil.isPractice(practice);
        if(errorResult!=null)return errorResult;
//        将题目添加到数据库中
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
//        补齐题目信息
        Long practiceId=practiceService.insert(loginInfo.getId(),practice);
        log.info("添加题目:{}",practice);
        return R.success(practiceId);
    }


    /**
     * 编辑题目 success
     * @param practice
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Practice practice){
//        查看修改后的题目是否合规
        R errorResult= PracticeUntil.isPractice(practice);
        if(errorResult!=null)return errorResult;
//        补齐题目信息
        practiceService.update(practice);
        log.info("编辑题目:{}",practice);
        return R.success();
    }

    /**
     * 单个删除 success
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
//        删除题目
        practiceService.delete(id);
//        删除题目对应的图片
        LambdaQueryWrapper<PracticeImage> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PracticeImage::getId,id);
        practiceImageService.remove(queryWrapper);
        log.info("删除题目:{}",id);
        return R.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deletelist")
    public R deleteList(Long[] ids) {
 //       删除题目
        List<Long> idList= Arrays.asList(ids);
        practiceService.deleteList(idList);
//        删除题目对应的图片
        LambdaQueryWrapper<PracticeImage> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(PracticeImage::getId,idList);
        practiceImageService.remove(queryWrapper);
        log.info("批量删除题目:{}", idList);
        return R.success();
    }


    /**
     * 上传多张图片
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadimage")
    public R uploadImage(@RequestParam("file") List<MultipartFile> file, HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        List<String> addresses=ossService.uploadList(file);
        List<PracticeImage> practiceImageList=new ArrayList<>();
        for (String address:addresses){
            practiceImageList.add(new PracticeImage(loginInfo.getId(),address));
        }
        practiceImageService.saveBatch(practiceImageList);
        return R.success();
    }

    /**
     * 获取所有专业
     * @return
     */
    @GetMapping("/list/subject")
    public R listBySubject(){
        List<Subject> list=subjectService.list();
        return R.success(list);
    }


    /**
     * 提交答案
     * @param id
     * @param answer
     * @return
     */
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable("id") Long id,@RequestParam String answer,HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        Practice practice=practiceService.getById(id);
        PracticeHistory practiceHistory=new PracticeHistory(practice,loginInfo.getId(),answer);
        if(practice.getAnswer().equals(answer)){
            practiceHistory.setStatus(1);
            practiceHistoryService.save(practiceHistory);
            return R.success();
        }
        else {
            practiceHistory.setStatus(0);
//            添加到历史记录中
            practiceHistoryService.save(practiceHistory);
            return R.error("答案错误");
        }
    }
}
